Skip to content

BUG: Slicing non monotonic DatetimeIndex did not raise for non-existing keys #37796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from

Conversation

phofl
Copy link
Member

@phofl phofl commented Nov 12, 2020

The test for historical reasons :) failed after the change. Is this a problem?

@phofl phofl added the Indexing Related to indexing on series/frames, not to indexes themselves label Nov 12, 2020
@jreback jreback added the Error Reporting Incorrect or improved errors from pandas label Nov 13, 2020
@jreback jreback added this to the 1.2 milestone Nov 13, 2020
@@ -1563,6 +1563,15 @@ def test_loc_getitem_slice_label_td64obj(self, start, stop, expected_slice):
expected = ser.iloc[expected_slice]
tm.assert_series_equal(result, expected)

def test_loc_getitem_slice_unordered_dt_index(self, frame_or_series):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the str index example from the OP as well. ping on green.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly enough this does not raise any more. I will open an issue for this. We have to define if slice_locs should raise too or if we should doo this after calling the function. Is not related to the DateTime fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche jorisvandenbossche changed the title [BUG]: Slicing non monotonic DatetimeIndex did not raise for non-existing keys BUG: Slicing non monotonic DatetimeIndex did not raise for non-existing keys Nov 13, 2020
@jorisvandenbossche
Copy link
Member

If there are "historical reasons", then we could also deprecate it first.
Also, we should clean up the comments about this (as they are now no longer correct)

@phofl can you also add a test where the resulting dataframe would not be empty? So where eg the start label is missing but would fall somewhere in the middle of the range of the index labels

@jreback
Copy link
Contributor

jreback commented Nov 13, 2020

If there are "historical reasons", then we could also deprecate it first.
Also, we should clean up the comments about this (as they are now no longer correct)

@phofl can you also add a test where the resulting dataframe would not be empty? So where eg the start label is missing but would fall somewhere in the middle of the range of the index labels

i am not sure there is anything to deprecate, this is just a bug

@jorisvandenbossche
Copy link
Member

It's also explicitly tested behaviour (I don't know the motivations of the PR that added this test, though)

@jreback
Copy link
Contributor

jreback commented Nov 13, 2020

It's also explicitly tested behaviour (I don't know the motivations of the PR that added this test, though)

its wrong it should raise, pls see the OP

@phofl
Copy link
Member Author

phofl commented Nov 13, 2020

Sorry for the confusion with the other issue. It raises for non-monotonic indexes and not for monotonic indexes

index=[Timestamp("2017"), Timestamp("2019"), Timestamp("2018")],
)
with pytest.raises(KeyError, match=r"Timestamp\('2020-01-01 00:00:00'\)"):
obj.loc["2020":"2022"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add an example where the label falls within the range of the Index?
Eg with the index being [2016, 2019, 2017] and then the slice "2018":"2020" or so

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, because of

# For historical reasons DatetimeIndex by default supports

Should we remove or deprecate? Harder fix than the previous one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, i guess we could try to deprecate, @jorisvandenbossche you feel strongly here?

@jorisvandenbossche
Copy link
Member

its wrong it should raise, pls see the OP

I agree the behaviour seems wrong and we should change it. To be clear I am not arguing for not having it raise an error. I am only saying that if this is explicitly tested behaviour, it might be depended upon (again, I don't know when / for what reason this test was added), and we can consider deprecating it first.

@jreback
Copy link
Contributor

jreback commented Nov 13, 2020

its wrong it should raise, pls see the OP

I agree the behaviour seems wrong and we should change it. To be clear I am not arguing for not having it raise an error. I am only saying that if this is explicitly tested behaviour, it might be depended upon (again, I don't know when / for what reason this test was added), and we can consider deprecating it first.

fair. ok let's see if possible to deprecate. though am tempted to call this wrong and just a bug fix but let's see.

@phofl
Copy link
Member Author

phofl commented Nov 13, 2020

Opened #37819 to deprecate this. If we decide to deprecate first, we can close this one.

@jreback
Copy link
Contributor

jreback commented Nov 15, 2020

closing in favor of #37819

@jreback jreback closed this Nov 15, 2020
@phofl phofl deleted the 18531 branch April 27, 2023 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent handling of empty slices with non-monotonic indexes
3 participants